/**
 *
 */
package com.jycode.commons;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

/**
 * @author "banlyst <banlyst@gmail.com>"
 *
 */
public class DeepCloneUtil {
	/**
	 * Allows all Elements the ability to be cloned.
	 * from: http://jakarta.apache.org/ecs/, source code of ECS
	 * 此函数适用于对POJO类进行深度克隆.
	 */
	public static Object clone(Object oThis) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(baos);
			out.writeObject(oThis);
			out.close();
			ByteArrayInputStream bin = new ByteArrayInputStream(baos
					.toByteArray());
			ObjectInputStream in = new ObjectInputStream(bin);
			Object clone = in.readObject();
			in.close();
			return (clone);
		} catch (ClassNotFoundException cnfe) {
			throw new InternalError(cnfe.toString());
		} catch (StreamCorruptedException sce) {
			throw new InternalError(sce.toString());
		} catch (IOException ioe) {
			throw new InternalError(ioe.toString());
		}
	}
}
